package com.wf.dal.config;

import com.alibaba.druid.pool.DruidDataSource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import javax.sql.DataSource;
import java.sql.SQLException;
import java.util.Map;

/**
 * @Description: 多数据源配置，主备数据源配置
 * @Author: EamonSec
 * @Company: shinyway
 * @Date: 2017/12/10 13:27
 */
@EnableTransactionManagement
@Configuration
@ConfigurationProperties(prefix = "druid")
public class DataSourceConfiguration {
    private final Logger logger = LoggerFactory.getLogger(DataSourceConfiguration.class);
    private Map<String, Object> master;


    private DataSource createDruidDataSource(Map<String, Object> map){

        DruidDataSource dataSource = DruidDataSourceBuilder.dataSourceBuilder(map);
        try {
            dataSource.init();
        } catch (SQLException e) {
            logger.error("数据库初始化异常",e);
        }
        return dataSource;
    }

    @Bean
    @Primary
    public DataSource masterDataSource() {
        return createDruidDataSource(master);
    }


    public Map<String, Object> getMaster() {
        return master;
    }

    public void setMaster(Map<String, Object> master) {
        this.master = master;
    }
}
